#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'walkingsky tangxn_1@163.com'


import json
import os
import re
import time
from models.huoban_api import HuobanApi
from models.config import Config
from models.excel_lib import read_excel
from models.log import Log

row_case_id = 0
row_run = 1
row_opration = 2
row_remark = 3
row_space_id = 4
row_table_name = 5
row_data = 6

class HuobanTest:

    def __init__(self):
        '''

        '''
        self.log = Log()
        self.huobanapi = HuobanApi()

    def get_case_files(self, path):
        #print(path)
        files = os.listdir(path)
        for file in files:
            if not os.path.isdir(file):
                
                if re.match(r'^[^~\$]*\.[xls]*',file) :
                    print(file)
                    self.log.set_log_file(file)
                    i = 1
                    done = False
                    while done == False:                            
                        row = read_excel(path+'\\'+file,i)
                        if not row == False :
                            self.run_case(row)
                        else:
                            done = True
                        i = i + 1
            else:
                self.get_case_files(path+'\\'+file)

    def run_case(self,row):
        print(row)
        if  not ( row[row_run] == 'Y' or row[row_run] == 'y' ):
            return
        if row[row_opration] == 'login':
            token = self.huobanapi.login(row[row_space_id],row[row_table_name])
            if len(token) > 10 :
                #print(token)
                self.log.write_log([row[row_case_id],'login',row[row_remark],'登录成功'])
            else:
                self.log.write_log([row[row_case_id],'login',row[row_remark],'登录失败'])
        elif row[row_opration] == 'create':
            space_id = row[row_space_id]
            table_name = row[row_table_name]
            self.huobanapi.set_headers(space_id)
            if self.huobanapi.write_data(table_name,row[row_data]) :
                self.log.write_log([row[row_case_id],'create',row[row_remark],'创建成功'])   
            else:
                self.log.write_log([row[row_case_id],'create',row[row_remark],'创建失败'])
        elif row[row_opration] == 'creates':
            space_id = row[row_space_id]
            table_name = row[row_table_name]
            self.huobanapi.set_headers(space_id)
            result = self.huobanapi.write_datas(table_name,row[row_data])            
            self.log.write_log([row[row_case_id],'creates',row[row_remark],'数据总条数：'+ str( result[0] ) + ',创建成功：' + str(result[1])])   
           
        elif row[row_opration] == 'modify_by_id':
            pass

        elif row[row_opration] == 'modify_by_filter':            
            space_id = row[row_space_id]
            table_name = row[row_table_name]
            self.huobanapi.set_headers(space_id)
            if self.huobanapi.modify_datas(table_name,row[row_data]) :
                self.log.write_log([row[row_case_id],'modify_by_filter',row[row_remark],'批量修改成功'])   
            else:
                self.log.write_log([row[row_case_id],'modify_by_filter',row[row_remark],'批量修改成失败'])

        elif row[row_opration] == 'delete_by_id':
            pass
        elif row[row_opration] == 'delete_by_filter':
            space_id = row[row_space_id]
            table_name = row[row_table_name]
            self.huobanapi.set_headers(space_id)
            if self.huobanapi.delete_datas(table_name,row[row_data]) :
                self.log.write_log([row[row_case_id],'delete_by_filter',row[row_remark],'删除提交成功'])   
            else:
                self.log.write_log([row[row_case_id],'delete_by_filter',row[row_remark],'删除提交失败'])
            pass
        elif row[row_opration] == 'clean_table':
            pass
        elif row[row_opration] == 'find_date':
            space_id = row[row_space_id]
            table_name = row[row_table_name]
            self.huobanapi.set_headers(space_id)
            result = self.huobanapi.find_datas(table_name,row[row_data])
            if result != False :
                self.log.write_log([row[row_case_id],'find_date',row[row_remark],'找到了'+ str(result[1])+'条数据，共'+str(result[0])+'条'])   
            else:
                self.log.write_log([row[row_case_id],'find_date',row[row_remark],'查找失败'])
            
        elif row[row_opration] == 'sleep':
            second = row[row_space_id]
            time.sleep(int(second))
            self.log.write_log([row[row_case_id],'sleep',row[row_remark],'等待'+second+'秒钟'])

    def run(self):
        '''
        执行测试
        '''
        #print(self.is_dev)
        self.get_case_files(Config().get_case_path())
        #read_excel(Config().get_case_path)
